package com.kaka.bus.engine.repay.detail;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.RepayInfo;
import com.kaka.bus.bean.Single;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.repay.RepayActivity;
import com.kaka.bus.util.U;
import com.kaka.bus.view.MutipleLayout;

import java.util.Date;

import butterknife.BindView;

/**
 * Created by OnionMac on 2018/1/18.
 */

public class RepayDetailActivity extends BaseActivity<RepayDetailPresenter> implements RepayDetailContract.View {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar_rl)
    RelativeLayout mToolbarRl;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.pact_progressbar)
    ProgressBar mPactProgressbar;
    @BindView(R.id.pact_webview)
    WebView mPactWebview;
    @BindView(R.id.pact_muti)
    MutipleLayout mPactMuti;
    /**
     * 给后台的数据 用户实际还款减掉2元
     */
    private String mRepay_money;

    public static final String REPAY_MONEY = "repay_money";
    public static final String BORROW_ID = "borrow_id";
    public static final String BANK_ID = "bank_id";
    public static final String ACT_REPAY_MONEY = "ACT";

    /**
     * 用户的实际还款
     */
    private String mPhone;
    private String mSerial;
    private boolean mIsRepaySuccess;

    private String mBorrowId;
    private String mBankId;
    private String mBankNum;
    private int mType;
    /**
     * 还款方式
     */
    private final String mTriggerStyle = "3";

    private String mActMoney;
    private RepayInfo mRepayInfo;

    @Override
    protected void initView() {
        super.initView();
        mToolbarBack.setOnClickListener(v -> finish());
        mToolbarName.setText("付款");

        mPactMuti.setStatus(MutipleLayout.Success);
        U.initWebView(mPactWebview,mPactProgressbar);

        /**
         * js
         */
        mPactWebview.addJavascriptInterface(new JSInterface(),"yhs");
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        mRepay_money = intent.getStringExtra(REPAY_MONEY);
        mBorrowId = intent.getStringExtra(BORROW_ID);
        mBankId = intent.getStringExtra(BANK_ID);
        mActMoney = intent.getStringExtra(ACT_REPAY_MONEY);
        mType = intent.getIntExtra(RepayActivity.TYPE, 0);

        String params = Constant.USERID+"="+getUserId()+"&amount="+mActMoney+"&_t="+new Date().getTime();

        mPactWebview.loadUrl(Constant.BASE_URL+Constant.REPAY_PAGE+params);

        mPresenter.getRepayInfo(getUserId(),getToken());
    }

    class JSInterface{

        @JavascriptInterface
        public void sendMsg(){
            runOnUiThread(() -> {
                if(mRepayInfo != null){
                    mPresenter.sendyzm(mRepayInfo.getPhone(), AppCenter.getTokenKey(),mBankNum);
                }
            });
        }

        @JavascriptInterface
        public void repay(String code){
            runOnUiThread(() -> {
                mPresenter.commitRepayInfo(mPhone,mBankId,mBankNum,mBorrowId,mRepay_money,mTriggerStyle,mType,code); //TODO 正式金额必须改回来mActMoney
            });
        }
    }

    public void BQSLose() {
        U.showDialogJuJue(this,Constant.REFUES);
    }

    @Override
    public void faild() {
        showMessage("发送验证码失败");
    }

    @Override
    public void getInfoSuccess(HttpWrapper<RepayInfo> data) {
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            mRepayInfo = data.data;
            showData();
        }else if(Constant.TOKEN_TIMEOUT.equals(data.code)){
            U.tokenOut(this);
        } else {
            showMessage(data.getInfo());
        }
    }

    /**
     * 赋值参数
     */
    private void showData() {
        mSerial = mRepayInfo.getSerial();
        mBankNum = mRepayInfo.getBankCard();
        mPhone = mRepayInfo.getPhone();
    }

    @Override
    public void showRepayResult(HttpWrapper<String> data) {
        mPactWebview.loadUrl("javascript:repayResult("+"'"+data.getCode()+"'"+")");
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            setResult(Constant.PAY_SUCCESS);
            mIsRepaySuccess = true;
            U.getSingleDialog(this, "温馨提示", data.getInfo(), "确定",
                    this::finish).show();
            return;
        } else if (Constant.PAY_WAITING.equals(data.code)) {

            setResult(Constant.PAY_SUCCESS);
            mIsRepaySuccess = true;
            U.getSingleDialog(this, "温馨提示", data.getInfo(), "确定",
                    this::finish).show();
            return;
        } else if (Constant.TOKEN_TIMEOUT.equals(data.code)) {
            setResult(Constant.PAY_FAILD);
        } else if(Constant.VERTIFY_ERROR.equals(data.code)){
            showMessage(data.getInfo());
            return ;
        }
        U.getSingleDialog(this, "温馨提示", data.info, "确定", this::finish).show();

    }

    @Override
    public void getCodeSuccess(HttpWrapper<Single> wrapper) {
        if (Constant.BQSLOSE_CODE.equals(wrapper.code)) {
            BQSLose();
        } else if (Constant.SUCCESS_CODE.equals(wrapper.code)) {
            showMessage("验证码发送成功");
        } else {
            faild();
            showMessage(wrapper.info);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_h5;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

}
